﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using ComicIsland.App.CIHelper;
using ComicIsland.App.Resources;
using ComicIsland.Common;
using ComicIsland.Common.Entities.InterfaceJson;
using ComicIsland.Common.WebHelper;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace ComicIsland.App.Views
{
    public partial class SearchFilterPage : PhoneApplicationPage
    {
        private bool IsLoad_InitPopupFilterCondition = false;
        private int themeId = -1, nationId = -1, progressId = -1, lengthId = -1, updatetimeId = -1;
        private ObservableCollection<ComicsList> hotComics = new ObservableCollection<ComicsList>();

        private int pageIndex = 1;
        private ComicsList currentSelectedItem = null;
        private bool IsLastPage = false;
        // 数据加载的标识
        public bool IsLoading = false;
        // 线程锁的对象
        private object o = new object();

        private bool IsNeedReloadData = false;

        public SearchFilterPage()
        {
            InitializeComponent();
            this.hotComics = new LocalHotComicHelper().GetHotComics();
            IsNeedReloadData = true;
            LoadSearchResult();
        }

        private void PhoneApplicationPage_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //this.popupFilterCondition.IsOpen = false;
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            IsNeedReloadData = false;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string argumentSubject = string.Empty;
            base.NavigationContext.QueryString.TryGetValue("subject", out argumentSubject);
            int.TryParse(argumentSubject, out themeId);
            if (themeId <= 0)
                themeId = -1;
            string argumentTitle = string.Empty;
            base.NavigationContext.QueryString.TryGetValue("title", out argumentTitle);
            if (!string.IsNullOrWhiteSpace(argumentTitle))
                this.txtTitle.Text = argumentTitle;
            //
            if (this.IsNeedReloadData)
                LoadSearchResult(themeId, nationId, progressId, lengthId, updatetimeId, string.Empty);
        }

        #region ******************************搜索结果******************************
        void LoadSearchResult(int pageIndex = 0)
        {
            LoadSearchResult(-1, -1, -1, -1, -1, string.Empty);
        }
        void LoadSearchResult(int themeId, int nationId, int progressId, int lengthId, int updatetimeId, string searchKeyword, int pageIndex = 0)
        {
            this.psbStatus.OpenStatusBar(AppResources.DataLoading);
            if (pageIndex <= 0)//重置搜索条件后重新加载数据
            {
                this.lls.ItemsSource = new ObservableCollection<ComicsList>();
            }
            string requestUrl = string.Concat(KEYS.REQUEST_HOST, "/comic/comicslist_v2", "?", KEYS.REQUEST_QUERY_PLATFORM);
            LogHelper.LogInfo(requestUrl + "\tPageIndex=" + pageIndex, this.GetType(), MethodBase.GetCurrentMethod());
            LogHelper.LogInfo(string.Format("Parameters:\tthemeId = {0}, nationId ={1}, progressId = {2}, lengthId = {3}, updatetimeId = {4}", themeId, nationId, progressId, lengthId, updatetimeId), this.GetType(), MethodBase.GetCurrentMethod());
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("sort", 0);
            parameters.Add("pageno", pageIndex);
            parameters.Add("pagesize", 20);
            if (themeId >= 0)
                parameters.Add("subject", themeId);
            if (nationId >= 0)
                parameters.Add("area", nationId);
            if (progressId >= 0)
                parameters.Add("progresstype", progressId);
            if (lengthId >= 0)
                parameters.Add("sizetype", lengthId);
            if (updatetimeId >= 0)
                parameters.Add("update", updatetimeId);
            if (!string.IsNullOrWhiteSpace(searchKeyword))
                parameters.Add("keyword", searchKeyword);

            PostClientHelper proxy = new PostClientHelper(parameters);
            //
            string cacheDataKey = string.Concat(KEYS.CacheJsonPrefix, MD5.GetMd5String(string.Concat(requestUrl, proxy.PostData.ToString())));
            LocalCacheDataHelper cacheHelper = new LocalCacheDataHelper();
            cacheHelper.GetDataFromLocalCache(cacheDataKey);
            if (!cacheHelper.IsReload)
            {
                var rootCharacterString = JSONHelper.Deserialize<JsonChannelMoreBase>(cacheHelper.LocalCacheData);
                if (!(rootCharacterString != null && rootCharacterString.Info != null && rootCharacterString.Info.ComicsList != null && rootCharacterString.Info.ComicsList.Count > 0))
                {
                    //MessageToastManager.Show("返回结果为空！", MessageToastLevel.Error);
                }
                else
                {
                    var items = rootCharacterString.Info.ComicsList;
                    if (items != null && items.Count > 0)
                    {
                        BindSearchResult(items);
                    }
                }
                this.psbStatus.CloseStatusBar();
                return;
            }

            proxy.DownloadStringCompleted += (sender, e) =>
            {
                try
                {
                    if (e.Error != null)
                    {
                        LogHelper.LogError(e.Error.ToString(), this.GetType(), MethodBase.GetCurrentMethod());
                        return;
                    }

                    #region 获取结果处理
                    string result = e.Result;

                    if (string.IsNullOrWhiteSpace(result))
                    {
                        LogHelper.LogError("Result is empty.");
                        //MessageToastManager.Show("未获取到数据！", MessageToastLevel.Error);
                        return;
                    }
                    //
                    if (result.StartsWith("exception:"))
                    {
                        LogHelper.LogError(result);
                        //MessageToastManager.Show("网络连接失败！", MessageToastLevel.Error);
                        return;
                    }
                    LogHelper.LogInfo(result, this.GetType(), MethodBase.GetCurrentMethod());
                    //
                    if (result.IndexOf("\"comicsList\":{}") >= 0)
                    {
                        this.IsLastPage = true;
                        return;
                    }
                    //
                    var rootCharacterString = JSONHelper.Deserialize<JsonChannelMoreBase>(result);
                    if (!(rootCharacterString != null && rootCharacterString.Info != null && rootCharacterString.Info.ComicsList != null && rootCharacterString.Info.ComicsList.Count > 0))
                    {
                        //MessageToastManager.Show("返回结果为空！", MessageToastLevel.Error);
                    }
                    else
                    {
                        var items = rootCharacterString.Info.ComicsList;
                        if (items != null && items.Count > 0)
                        {
                            cacheHelper.SetDataToLocalCache(cacheDataKey, result);
                            BindSearchResult(items);
                        }
                    }

                    #endregion 获取结果处理
                }
                catch (Exception err)
                {
                }
                finally
                {
                    this.psbStatus.CloseStatusBar();
                }
            };
            proxy.DownloadStringAsync(new Uri(requestUrl, UriKind.Absolute));
        }

        void BindSearchResult(ObservableCollection<ComicsList> items)
        {
            if (!(items != null && items.Count > 0))
                return;
            this.Dispatcher.BeginInvoke(() =>
            {
                if (pageIndex > 0)
                {
                    if (this.lls.ItemsSource != null && this.lls.ItemsSource.Count > 0)
                    {
                        foreach (var item in items)
                        {
                            this.lls.ItemsSource.Add(item);
                        }
                    }
                    else
                    {
                        this.lls.ItemsSource = items;
                    }
                }
                else
                {
                    this.lls.ItemsSource = items;
                }
            });
        }

        private void Grid_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
        {
            e.Complete();
            if (e.CumulativeManipulation.Translation.Y > 0)
            {
                //从下往上拉时不处理
                return;
            }

            if (IsLastPage)
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    Helper.PageShowDataNoMore<ComicsList>(sender, this.lls);
                });
            }
        }

        void ExecuteFilterCondition()
        {
            var childrens = this.wpFilterCondition.Children;
            if (!(childrens != null && childrens.Count > 0))
            {
                //无条件
                LoadSearchResult();
                return;
            }


            for (int i = 0; i < childrens.Count; i++)
            {
                Grid grid = childrens[i] as Grid;
                if (!(grid != null && grid.DataContext != null))
                    continue;

                if (grid.DataContext is SubjectList)
                {
                    themeId = (grid.DataContext as SubjectList).SubjectId;
                    this.txtTitle.Text = (grid.DataContext as SubjectList).SubjectName;
                }
                if (grid.DataContext is JsonSearchFilterConditionNation)
                {
                    nationId = (grid.DataContext as JsonSearchFilterConditionNation).Id;
                }
                if (grid.DataContext is JsonSearchFilterConditionProgress)
                {
                    progressId = (grid.DataContext as JsonSearchFilterConditionProgress).Id;
                }
                if (grid.DataContext is JsonSearchFilterConditionLength)
                {
                    lengthId = (grid.DataContext as JsonSearchFilterConditionLength).Id;
                }
                if (grid.DataContext is JsonSearchFilterConditionUpdateTime)
                {
                    updatetimeId = (grid.DataContext as JsonSearchFilterConditionUpdateTime).Id;
                }
            }

            LoadSearchResult(themeId, nationId, progressId, lengthId, updatetimeId, string.Empty);
        }

        private void lls_ItemRealized(object sender, ItemRealizationEventArgs e)
        {
            lock (o)
            {
                if (IsLoading)
                    return;
                if (e.ItemKind != LongListSelectorItemKind.Item)
                    return;
                //
                MarkHotComicFlag(e.Container);
                //
                currentSelectedItem = e.Container.Content as ComicsList;
                int selectedIndex = this.lls.ItemsSource.IndexOf(currentSelectedItem);
                int total = this.lls.ItemsSource.Count;
                if (total - selectedIndex <= 2)
                {
                    // 设置IsLoading为true，在加载数据的过程中，禁止多次进入
                    IsLoading = true;
                    if (IsLastPage)
                    {
                        //this.statusBar.OpenStatusBar(AppResources.DataNoMore);
                        //this.statusBar.CloseStatusBar();
                        MessageToastManager.Show(AppResources.DataNoMore);
                        // 修改加载的状态
                        IsLoading = false;
                    }
                    else
                    {
                        this.pageIndex += 1;
                        this.LoadSearchResult(this.themeId, this.nationId, this.progressId, this.lengthId, this.updatetimeId, string.Empty, pageIndex);
                        // 修改加载的状态
                        IsLoading = false;
                    }
                }
            }
        }

        void MarkHotComicFlag(ContentPresenter contentPresenter)
        {
            try
            {
                var comic = contentPresenter.Content as ComicsList;
                if (comic == null)
                    return;
                var images = Helper.FindChildren<Image>(contentPresenter);
                if (!(images != null && images.Count() > 0))
                    return;

                foreach (Image image in images)
                {
                    if (image == null)
                        continue;
                    if ("imageHotFlag".Equals(image.Name))
                    {
                        var item = (from p in this.hotComics where p.BigbookId == comic.BigbookId select p).FirstOrDefault<ComicsList>();
                        if (item != null)
                            image.Visibility = System.Windows.Visibility.Visible;
                        else
                            image.Visibility = System.Windows.Visibility.Collapsed;
                    }
                }
            }
            catch { }
        }

        private void gridSearchResult_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var item = (sender as Grid).DataContext as ComicsList;
            if (item == null)
                return;
            NaviService.Navigate(ComicIslandPage.ComicSummaryPage, string.Concat("?comicId=", item.BigbookId));
        }
        #endregion ******************************搜索结果******************************










        #region ******************************加载查询条件数据******************************
        void LoadLocalSearchFilterCondition()
        {
            this.llsSearchNation.ItemsSource = JsonSearchFilterCondition.InitJsonSearchFilterConditionNation();
            this.llsSearchProgress.ItemsSource = JsonSearchFilterCondition.InitJsonSearchFilterConditionProgress();
            this.llsSearchLength.ItemsSource = JsonSearchFilterCondition.InitJsonSearchFilterConditionLength();
            this.llsSearchUpdateTime.ItemsSource = JsonSearchFilterCondition.InitJsonSearchFilterConditionUpdateTime();
        }
        void LoadTheme()
        {
            string requestUrl = string.Concat(KEYS.REQUEST_HOST, "/comic/sorttypelist");
            LogHelper.LogInfo(requestUrl, this.GetType(), MethodBase.GetCurrentMethod());
            WebTaskHelper webTask = new WebTaskHelper(new Uri(requestUrl));
            IAsyncResult ia = webTask.BeginWebTask(delegate(IAsyncResult callBackResult)
            {
                try
                {
                    this.Dispatcher.BeginInvoke(() =>
                    {
                        #region 获取结果处理
                        string result = webTask.EndWebTask(callBackResult);

                        if (string.IsNullOrWhiteSpace(result))
                        {
                            LogHelper.LogError("Result is empty.");
                            //MessageToastManager.Show("未获取到数据！", MessageToastLevel.Error);
                            return;
                        }
                        //
                        if (result.StartsWith("exception:"))
                        {
                            LogHelper.LogError(result);
                            //MessageToastManager.Show("网络连接失败！", MessageToastLevel.Error);
                            return;
                        }
                        LogHelper.LogInfo(result, this.GetType(), MethodBase.GetCurrentMethod());
                        //
                        var rootCharacterString = JSONHelper.Deserialize<JsonComicSearchTheme>(result);
                        if (!(rootCharacterString != null && rootCharacterString.Info != null && rootCharacterString.Info != null && rootCharacterString.Info.SubjectList != null && rootCharacterString.Info.SubjectList.Count > 0))
                        {
                            //MessageToastManager.Show("返回结果为空！", MessageToastLevel.Error);
                        }
                        else
                        {
                            this.llsSearchTheme.ItemsSource = rootCharacterString.Info.SubjectList;
                        }

                        #endregion 获取结果处理
                    });
                }
                catch (Exception err)
                {

                }
            }, webTask);
        }
        #endregion ******************************加载查询条件数据******************************


        private void appBarBack_Click(object sender, EventArgs e)
        {

        }

        private void appBarSearch_Click(object sender, EventArgs e)
        {
            if (!this.popupFilterCondition.IsOpen)
            {
                this.popupFilterCondition.Visibility = Visibility.Visible;
                this.popupFilterCondition.IsOpen = true;
            }
            else
            {
                this.popupFilterCondition.Visibility = Visibility.Collapsed;
                this.popupFilterCondition.IsOpen = false;
            }

            if (!IsLoad_InitPopupFilterCondition)
            {
                IsLoad_InitPopupFilterCondition = true;
                LoadTheme();
                LoadLocalSearchFilterCondition();
            }
        }

        #region  ******************************搜索过滤******************************
        void SetLongListSelectorVisibility(Visibility theme, Visibility nation, Visibility progress, Visibility length, Visibility updatetime)
        {
            this.llsSearchTheme.Visibility = theme;
            this.llsSearchNation.Visibility = nation;
            this.llsSearchProgress.Visibility = progress;
            this.llsSearchLength.Visibility = length;
            this.llsSearchUpdateTime.Visibility = updatetime;
        }


        private void RadioButton_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            RadioButton rdb = (sender as RadioButton);
            if (!(rdb != null))
            {
                return;
            }

            switch (rdb.Content.ToString())
            {
                case "国家":
                    this.SetLongListSelectorVisibility(Visibility.Collapsed, Visibility.Visible, Visibility.Collapsed, Visibility.Collapsed, Visibility.Collapsed);
                    break;
                case "进度":
                    this.SetLongListSelectorVisibility(Visibility.Collapsed, Visibility.Collapsed, Visibility.Visible, Visibility.Collapsed, Visibility.Collapsed);
                    break;
                case "长度":
                    this.SetLongListSelectorVisibility(Visibility.Collapsed, Visibility.Collapsed, Visibility.Collapsed, Visibility.Visible, Visibility.Collapsed);
                    break;
                case "更新时间":
                    this.SetLongListSelectorVisibility(Visibility.Collapsed, Visibility.Collapsed, Visibility.Collapsed, Visibility.Collapsed, Visibility.Visible);
                    break;
                case "题材":
                default:
                    this.SetLongListSelectorVisibility(Visibility.Visible, Visibility.Collapsed, Visibility.Collapsed, Visibility.Collapsed, Visibility.Collapsed);
                    break;
            }
        }

        #region ********************条件选择********************
        private bool IsExistWrapPanelTheme(LongListSelector lls, RadioButton rdb)
        {
            bool result = false;
            var items = lls.ItemsSource as ObservableCollection<SubjectList>;
            var childrens = this.wpFilterCondition.Children;
            for (int i = 0; i < childrens.Count; i++)
            {
                Grid grid = childrens[i] as Grid;
                SubjectList currentValue = grid.DataContext as SubjectList;
                if (currentValue == null)
                    continue;
                var item = (from p in items where p.SubjectName.Equals(currentValue.SubjectName) select p).FirstOrDefault<SubjectList>();
                if (item != null)
                {
                    if (rdb.Content.ToString().Equals(currentValue.SubjectName))
                    {
                        result = true;
                        break;
                    }
                    else
                    {
                        this.wpFilterCondition.Children.Remove(childrens[i]);
                    }
                }
            }
            return result;
        }

        private bool IsExistWrapPanelUpdateTime(LongListSelector lls, RadioButton rdb)
        {
            bool result = false;
            var items = lls.ItemsSource as ObservableCollection<JsonSearchFilterConditionUpdateTime>;
            var childrens = this.wpFilterCondition.Children;
            for (int i = 0; i < childrens.Count; i++)
            {
                Grid grid = childrens[i] as Grid;
                var currentValue = grid.DataContext as JsonSearchFilterConditionUpdateTime;
                if (currentValue == null)
                    continue;
                var item = (from p in items where p.Name.Equals(currentValue.Name) select p).FirstOrDefault<JsonSearchFilterConditionUpdateTime>();
                if (item != null)
                {
                    if (rdb.Content.ToString().Equals(currentValue.Name))
                    {
                        result = true;
                        break;
                    }
                    else
                    {
                        this.wpFilterCondition.Children.Remove(childrens[i]);
                    }
                }
            }
            return result;
        }

        private bool IsExistWrapPanelLength(LongListSelector lls, RadioButton rdb)
        {
            bool result = false;
            var items = lls.ItemsSource as ObservableCollection<JsonSearchFilterConditionLength>;
            var childrens = this.wpFilterCondition.Children;
            for (int i = 0; i < childrens.Count; i++)
            {
                Grid grid = childrens[i] as Grid;
                var currentValue = grid.DataContext as JsonSearchFilterConditionLength;
                if (currentValue == null)
                    continue;
                var item = (from p in items where p.Name.Equals(currentValue.Name) select p).FirstOrDefault<JsonSearchFilterConditionLength>();
                if (item != null)
                {
                    if (rdb.Content.ToString().Equals(currentValue.Name))
                    {
                        result = true;
                        break;
                    }
                    else
                    {
                        this.wpFilterCondition.Children.Remove(childrens[i]);
                    }
                }
            }
            return result;
        }

        private bool IsExistWrapPanelProgress(LongListSelector lls, RadioButton rdb)
        {
            bool result = false;
            var items = lls.ItemsSource as ObservableCollection<JsonSearchFilterConditionProgress>;
            var childrens = this.wpFilterCondition.Children;
            for (int i = 0; i < childrens.Count; i++)
            {
                Grid grid = childrens[i] as Grid;
                var currentValue = grid.DataContext as JsonSearchFilterConditionProgress;
                if (currentValue == null)
                    continue;
                var item = (from p in items where p.Name.Equals(currentValue.Name) select p).FirstOrDefault<JsonSearchFilterConditionProgress>();
                if (item != null)
                {
                    if (rdb.Content.ToString().Equals(currentValue.Name))
                    {
                        result = true;
                        break;
                    }
                    else
                    {
                        this.wpFilterCondition.Children.Remove(childrens[i]);
                    }
                }
            }
            return result;
        }

        private bool IsExistWrapPanelNation(LongListSelector lls, RadioButton rdb)
        {
            bool result = false;
            var items = lls.ItemsSource as ObservableCollection<JsonSearchFilterConditionNation>;
            var childrens = this.wpFilterCondition.Children;
            for (int i = 0; i < childrens.Count; i++)
            {
                Grid grid = childrens[i] as Grid;
                var currentValue = grid.DataContext as JsonSearchFilterConditionNation;
                if (currentValue == null)
                    continue;
                var item = (from p in items where p.Name.Equals(currentValue.Name) select p).FirstOrDefault<JsonSearchFilterConditionNation>();
                if (item != null)
                {
                    if (rdb.Content.ToString().Equals(currentValue.Name))
                    {
                        result = true;
                        break;
                    }
                    else
                    {
                        this.wpFilterCondition.Children.Remove(childrens[i]);
                    }
                }
            }
            return result;
        }

        private void AddFilterConditionToWrapPanel(RadioButton rdb)
        {
            TextBlock txt = new TextBlock() { Text = rdb.Content.ToString(), HorizontalAlignment = HorizontalAlignment.Center };
            txt.Foreground = new SolidColorBrush(Helper.HexToColor(Convert.ToString(Application.Current.Resources["DefaultForegroundColor"])));
            Image img = new Image();
            BitmapImage bi = new BitmapImage() { UriSource = new Uri("/Images/tmp/x.png", UriKind.Relative) };
            img.Source = bi;
            img.Width = 32;
            Grid grid = new Grid() { Margin = new Thickness(0, 0, 0, 20), Width = 140 };
            grid.Background = new SolidColorBrush(Helper.HexToColor(Convert.ToString(Application.Current.Resources["DefaultContentWhiteColor"])));
            grid.RowDefinitions.Add(new RowDefinition());
            grid.RowDefinitions.Add(new RowDefinition());
            grid.DataContext = rdb.DataContext;
            Grid.SetRow(txt, 0);
            Grid.SetRow(img, 1);
            grid.Children.Add(txt);
            grid.Children.Add(img);
            grid.Tap += grid_Tap;
            this.wpFilterCondition.Children.Add(grid);
            //执行搜索条件
            ExecuteFilterCondition();
        }



        void grid_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Grid grid = sender as Grid;
            if (grid != null)
            {
                this.wpFilterCondition.Children.Remove(grid);
                //执行搜索条件

                if (grid.DataContext == null)
                    return;
                if (grid.DataContext is SubjectList)
                {
                    themeId = -1;
                    this.txtTitle.Text = "分类筛选";
                    SetUnCheckedToRadiaButton(this.llsSearchTheme);
                }
                if (grid.DataContext is JsonSearchFilterConditionNation)
                {
                    nationId = -1;
                    SetUnCheckedToRadiaButton(this.llsSearchNation);
                }
                if (grid.DataContext is JsonSearchFilterConditionProgress)
                {
                    progressId = -1;
                    SetUnCheckedToRadiaButton(this.llsSearchProgress);
                }
                if (grid.DataContext is JsonSearchFilterConditionLength)
                {
                    lengthId = -1;
                    SetUnCheckedToRadiaButton(this.llsSearchLength);
                }
                if (grid.DataContext is JsonSearchFilterConditionUpdateTime)
                {
                    updatetimeId = -1;
                    SetUnCheckedToRadiaButton(this.llsSearchUpdateTime);
                }

                ExecuteFilterCondition();
            }
        }

        void SetUnCheckedToRadiaButton(LongListSelector longListSelector)
        {
            if (VisualTreeHelper.GetChildrenCount(longListSelector) > 0)
            {
                IEnumerable<RadioButton> rdbs = Helper.FindChildren<RadioButton>(longListSelector);
                if (rdbs != null && rdbs.Count() > 0)
                {
                    foreach (RadioButton rdb in rdbs.ToList<RadioButton>())
                    {
                        if (rdb != null)
                            rdb.IsChecked = false;
                    }
                }
            }
        }

        #endregion ********************条件选择********************

        private void rdbSearchTheme_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            RadioButton rdb = sender as RadioButton;
            if (rdb != null)
            {
                if (!IsExistWrapPanelTheme(this.llsSearchTheme, rdb))
                    AddFilterConditionToWrapPanel(rdb);
            }
        }

        private void rdbSearchUpdateTime_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            RadioButton rdb = sender as RadioButton;
            if (rdb != null)
            {
                if (!IsExistWrapPanelUpdateTime(this.llsSearchUpdateTime, rdb))
                    AddFilterConditionToWrapPanel(rdb);
            }
        }

        private void rdbSearchLength_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            RadioButton rdb = sender as RadioButton;
            if (rdb != null)
            {
                if (!this.IsExistWrapPanelLength(this.llsSearchLength, rdb))
                    AddFilterConditionToWrapPanel(rdb);
            }
        }

        private void rdbSearchProgress_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            RadioButton rdb = sender as RadioButton;
            if (rdb != null)
            {
                if (!this.IsExistWrapPanelProgress(this.llsSearchProgress, rdb))
                    AddFilterConditionToWrapPanel(rdb);
            }
        }

        private void rdbSearchNation_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            RadioButton rdb = sender as RadioButton;
            if (rdb != null)
            {
                if (!this.IsExistWrapPanelNation(this.llsSearchNation, rdb))
                    AddFilterConditionToWrapPanel(rdb);
            }
        }
        #endregion  ******************************搜索过滤******************************

        /// <summary>
        /// 控件弹出搜索过滤条件的Popup位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.popupFilterCondition.HorizontalOffset = Application.Current.Host.Content.ActualWidth - this.gridPopupFilterCondition.ActualWidth;
        }

        private void lls_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.popupFilterCondition.IsOpen = false;
        }



    }
}